# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 11:47:55 2018

@author: jiujiujiu
"""

from cs import day
import requests
import time
from WriteData import writedata
from Tk import genearteMD5


# 获取单个页面
def postHTMLText(url, data):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:
        r = requests.post(url, data=data, headers=header)
        #        html = r.text
        html = r.content
        #        print(html)
        return html
    except:
        return None


# 提取具体数据(格式为列表元组)
def get_data1(url, data):
    l = []
    id = 0
    html = postHTMLText(url, data)
    if not html:
        return None
    html = eval(html)
    datas = html['data']
    for dt in datas:
        date = time.strftime("%Y-%m-%d %X", time.localtime(dt['createtime']))[:10]
        id = dt['news_id']
        l.append(date)
    return (l, id)


def get_data2(url, data):
    l = []
    id = 0
    html = postHTMLText(url, data)
    if not html:
        return None
    html = eval(html)
    datas = html['data']
    for dt in datas:
        date = time.strftime("%Y-%m-%d %X", time.localtime(dt['createtime']))[:10]
        id = dt['article_id']
        #        print(date)
        l.append(date)
    #    print(l,id)
    return (l, id)


def count_process_one():  # 快讯
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    id = 99999999

    while 1:
        url = 'http://www.fengniaocaijing.com/api/route/news/news-flash/records?'
        data = {
            'start': id,
            'limit': '30'
        }
        dt = get_data1(url, data)
        if not data:
            break
        datas = dt[0]
        id = dt[1]
        print('----蜂鸟财经快讯获取第{}页----'.format(a))
        for date in datas:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_two():  # t头条
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    id = 99999999
    while 1:
        url = 'http://www.fengniaocaijing.com/api/route/article/records?'
        data = {
            'category_id': '61',
            'start': id,
            ' limit': '30'
        }
        dt = get_data2(url, data)
        if not data:
            break
        datas = dt[0]
        id = dt[1]
        print('----蜂鸟财经头条获取第{}页----'.format(a))
        for date in datas:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '蜂鸟财经'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
